<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('字段排序设置')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-interface-add" th:object="${interfaceParamsFieldRelation}">
            <input type="hidden" id="id" name="id" th:value="${id}">
            <input type="hidden" id="type" name="type" th:value="${type}">
            <div class="form-group">
                <label class="col-sm-3 control-label">字段自定义模式：</label>
                <div class="col-sm-8">
                    <textarea name="fieldDescription" class="form-control">[[${interfaceParamsFieldRelation.fieldDescription}]]</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">出参判空策略：</label>
                <div class="col-sm-8">
                    <select name="isAllowEmpty" id="isAllowEmpty" class="form-control m-b" th:with="type=${@dict.getType('isAllowEmpty')}">
                        <option value="">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{isAllowEmpty}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group errorDiv" style="display: none">
                <label class="col-sm-3 control-label">错误码分类：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="treeId" name="errorCodeCategoryId" type="hidden" th:value="${interfaceParamsFieldRelation.errorCodeCategoryId}"/>
                        <input class="form-control" type="text" onclick="selectErrorCodeCategoryTree()" id="treeName" readonly="true" th:value="${errorCodeCategoryName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group errorDiv" style="display: none">
                <label class="col-sm-3 control-label">错误码：</label>
                <input name="errorCodeSet" type="hidden" th:value="${interfaceParamsFieldRelation.errorCode}"/>
                <div class="col-sm-8" id="errorCodeDiv">
                    <select class="form-control" name="errorCode" id="errorCode">
                        <option value="0">--请选择--</option>
                    </select>
                </div>
            </div>
            <div class="form-group" th:if="${isMethod} == true">
                <label class="col-sm-3 control-label">字段排序：</label>
                <div class="col-sm-8">
                    <select name="orderType" id="orderType" class="form-control m-b" th:with="type=${@dict.getType('orderType')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{orderType}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group" th:if="${isBff} == true">
                <label class="col-sm-3 control-label">字段中文别名：</label>
                <div class="col-sm-8">
                    <input type="text" name="aliasZhName" th:value="*{aliasZhName}" class="form-control"/>
                </div>
            </div>
            <div class="form-group" th:if="${isBff} == true">
                <label class="col-sm-3 control-label">字段英文别名：</label>
                <div class="col-sm-8">
                    <input name="aliasName" class="form-control" type="text" th:value="*{aliasName}">
                </div>
            </div>

            <div class="form-group" th:if="${isTree} == true">
                <label class="col-sm-3 control-label">树形模式：</label>
                <div class="col-sm-8">
                    <select name="treeModel" id="treeModel" class="form-control m-b" th:with="type=${@dict.getType('fieldTreeModel')}">
                        <option value="">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{treeModel}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group" th:if="${isOther} == true">
                <label class="col-sm-3 control-label">其它模式：</label>
                <div class="col-sm-8">
                    <select name="otherModel" id="otherModel" class="form-control m-b" th:with="type=${@dict.getType('fieldOtherModel')}">
                        <option value="">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{otherModel}"></option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "interface/interface";
        $("#form-interface-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/updateFieldDesign2", $('#form-interface-add').serialize());
            }
        }
        function resetHandler() {
            $.operate.save(prefix + "/resetFieldDesign2", $('#form-interface-add').serialize());
        }

        /*错误码分类-新增-选择父错误码分类树*/
        function selectErrorCodeCategoryTree() {
            var treeId = $("#treeId").val();
            if (!treeId) {
                treeId = "";
            }
            var options = {
                title: '错误码分类选择',
                width: "380",
                url:  ctx + "errorCodeCategory/errorCodeCategory" + "/selectErrorCodeCategoryTree/" + treeId,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
            queryErrorCodeList();
        }

        function queryErrorCodeList() {
            var treeId = $("#treeId").val();
            let config = {
                url: ctx + "errorCode/errorCode/queryErrorCodeById?errorCodeCategoryId=" + treeId,
                type: "get",
                dataType: "json",
                data: null,
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function (result) {
                    $.modal.closeLoading();
                    if (result.code == web_status.SUCCESS) {
                        var selectTplHtml = $("#selectTpl").html();
                        result.selectId= $("input[name='errorCodeSet']").val()||''
                        laytpl(selectTplHtml).render(result, function (html) {
                            $("#errorCodeDiv").empty();
                            $("#errorCodeDiv").append(html);
                        });
                        return false;
                    }
                    if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                        return false;
                    }
                    $.modal.alertError(result.msg);
                }
            };
            $.ajax(config);
        }

        $(function () {
            var treeId = $("#treeId").val();
            if (treeId) {
                queryErrorCodeList();
            }

            var value = $("#isAllowEmpty").val();
            if (value == 1 || value == '1') {
                $(".errorDiv").show();
            } else {
                $(".errorDiv").hide();
            }

            $("#isAllowEmpty").change(function () {
                var value = $(this).val();
                if (value == 1 || value == '1') {
                    $(".errorDiv").show();
                } else {
                    $(".errorDiv").hide();
                }
            })
        })
    </script>

    <script id="selectTpl" type="text/html">
        <select class="form-control errorCodeValue" name="errorCode">
            <option value="0">--请选择--</option>
            {{# layui.each(d.data, function(index, a){ }}
            <option value="{{a.errorCode}}"
                    {{#if(a.errorCode==d.selectId){}}selected{{# }}}>{{a.errorCode}}/{{a.errorCodeDetail}}</option>
            {{# }); }}
        </select>
    </script>
</body>
</html>